import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JRadioButton;
import javax.swing.JTextField;


public class RadioAuthSelect extends Frame{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	final JFrame frame = new JFrame();
	
	public void makeFrame(){
		final JPanel panel = new JPanel(new GridLayout(5,1));
		JPanel top = new JPanel(new GridLayout(2,1));
		JPanel topMiddle = new JPanel(new GridLayout(2,1));
		JPanel middle = new JPanel(new GridLayout(2,1));
		JPanel bottom = new JPanel(new GridLayout(1,3));
		JPanel save = new JPanel();
		
		final JRadioButton noAuth = new JRadioButton("No 2-Way Google Authentication");
		final JRadioButton auth = new JRadioButton("Use 2-Way Google Authentication");
		final JLabel user = new JLabel("Please enter GMail address:");
		final JTextField username = new JTextField(20);
		final JLabel oneTimePass = new JLabel("Please enter the one time pass:");
		final JPasswordField password = new JPasswordField(16);
		final JCheckBox savePass = new JCheckBox("Save Username and Password");
		JButton submit = new JButton("Submit");
		JButton noVis = new JButton();
		noVis.setVisible(false);
		user.setVisible(false);
		username.setVisible(false);
		oneTimePass.setVisible(false);
		password.setVisible(false);
		JButton cancel = new JButton("Cancel");
		top.add(noAuth);
		top.add(auth);
		topMiddle.add(user);
		topMiddle.add(username);
		middle.add(oneTimePass);
		middle.add(password);
		bottom.add(submit);
		bottom.add(noVis);
		bottom.add(cancel);
		save.add(savePass);
		noAuth.setSelected(true);
		LoginPrompt.useAuth = false;
		auth.addActionListener(new ActionListener(){

			@Override
			public void actionPerformed(ActionEvent e) {
				oneTimePass.setVisible(true);
				password.setVisible(true);
				user.setVisible(true);
				username.setVisible(true);
				panel.updateUI();
				noAuth.setSelected(false);
				LoginPrompt.useAuth = true;
				
			}
			
		});
		noAuth.addActionListener(new ActionListener(){

			@Override
			public void actionPerformed(ActionEvent e) {
				oneTimePass.setVisible(false);
				password.setVisible(false);
				user.setVisible(false);
				username.setVisible(false);
				panel.updateUI();
				auth.setSelected(false);
				LoginPrompt.useAuth = false;
			}
			
		});
		noAuth.setSelected(true);
		panel.setVisible(true);
		
		cancel.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent arg0) {
				frame.dispose();
			}
			
		});
		submit.addActionListener(new ActionListener(){
				/*
				 *Needs to write one-time password to file and then read it in if it needs it later on 
				 */
			@Override
			public void actionPerformed(ActionEvent e) {
				
				LoginPrompt prompt = new LoginPrompt();
				prompt.loginAttempt(username.getText(), password.getPassword());
				frame.dispose();
			}
			
		});
		
		savePass.addActionListener(new ActionListener(){

			@Override
			public void actionPerformed(ActionEvent e) {
				if(savePass.isSelected() == true)
					LoginPrompt.savePassword = true;
				if(savePass.isSelected() == false)
					LoginPrompt.savePassword = false;
				String user = username.getText();
				char at = '@';
				int index = user.indexOf(at);
				String u = user.substring(0,index);
				File file = new File(System.getProperty("user.home")+"\\EasyFTP\\Saved\\"+u+".txt");
				if(file.exists())
					file.delete();
				try {
					file.createNewFile();
					FileWriter stream = null;
					stream = new FileWriter(file);
					BufferedWriter out = new BufferedWriter(stream);
					out.write(user);
					out.newLine();
					out.write(password.getPassword());
					out.close();
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
			
		});
		panel.add(top);
		panel.add(topMiddle);
		panel.add(middle);
		panel.add(bottom);
		panel.add(save);
		frame.add(panel);
		frame.setSize(350,350);
		frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		frame.setVisible(true);
		frame.setLocationRelativeTo(null);
		frame.setTitle("GMail 2-Way Authentication");
	}
	
}
